import { Label, tween } from "cc";

export default class StringUtils {

    public static insertSeparator(number: number | string, separator = ',', radix = 3) {
        if (typeof number == 'number') number = number.toString();
        // let reg = /(?=(?!\b)(\d{3})+$)/g;
        let reg = new RegExp('(?=(?!\\b)(\\d{' + radix + '})+$)', 'g');
        return number.replace(reg, separator);
    }

    public static unitFormat(number: number, units: string[], radix = 3, fractionDigits = 0) {
        let base = Math.pow(10, radix);
        let unitIndex = -1;
        while (number > base && unitIndex < units.length) {
            number /= base;
            unitIndex += 1;
        }
        let unit = ''
        if (unitIndex != -1) unit = units[unitIndex];
        return number.toFixed(fractionDigits) + unit;
    }

    public static timestampToTime(timestamp?: number) {
        let date = timestamp ? new Date(timestamp) : new Date();
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 + "").padStart(2, "0") + '-';
        let D = (date.getDate() + "").padStart(2, "0") + ' ';
        let h = (date.getHours() + "").padStart(2, "0") + ':';
        let m = (date.getMinutes() + "").padStart(2, "0") + ':';
        let s = (date.getSeconds() + "").padStart(2, "0");
        return Y + M + D + h + m + s;
    }

    // 比较两个string数组是否相等
    public static judgeStringArrayEqual(array1: string[], array2: string[]) {
        if (array1.length !== array2.length) {
            return false
        }
        array1.sort()
        array2.sort()
        for (let i = 0; i < array1.length; i++) {
            if (array1[i] != array2[i]) {
                return false
            }
        }
        return true
    }
    public static removePointAftZero(str: string) {
        let tempStr = str;
        let index = str.indexOf(".");
        if (index != -1) {
            tempStr = tempStr.substring(index + 1);
            if (tempStr[0] == '0') {
                return str.substring(0, index);
            }
        }
        return str;
    }

    /**
     * 文字标签控件滚动数字
     * @param isAdd 是否是添加
     * @param label lable组件
     * @param addNum  需要加的数量
     * @param totalNum 当前总共数量
     * @param callback 滚动完回调
     * @param times 滚动几秒
     */
    public static rollNumLabelAtlas(isAdd: boolean, label: Label, addNum: number, totalNum: number, callback: Function, times: number = 6) {
        let num: number = addNum
        let count: number = times * 10
        let total: number = 0;
        let cellNum: number = num / count + 1
        if (num <= count) {
            count = num;
            cellNum = 1;
        } else {
            count = count + 1;
        }
        let _increaseNum = function () {
            if (isAdd) {
                if (total != num) {
                    total = total + cellNum;
                    if (total > num) {
                        cellNum = cellNum - (total - num)
                        total = num
                    }
                    let curNum = Number(label.string)
                    let showNum = Math.floor(curNum + cellNum).toFixed(0) + ""
                    label.string = showNum;
                }
            } else {
                if (total != num) {
                    total = total + cellNum;
                    if (total > num) {
                        cellNum = 0
                        total = num
                    }
                    let curNum = Number(label.string)
                    if (curNum > totalNum) {
                        let showNum = Math.floor(curNum - cellNum).toFixed(0) + ""
                        label.string = showNum;
                    }

                }
            }
        }
        if (count > 0) {
            tween(label.node).call(() => {
                _increaseNum()
            }).delay(0.05).union().repeat(count).call(() => {
                callback()
            }).start();
        } else {
            callback()
        }
    }
}